package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizErrorCode;
import com.atguigu.common.to.member.SocialUserTo;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UserNameExistException;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 会员
 *
 * @author zhough
 * @email zhough@gmail.com
 * @date 2024-01-22 20:23:32
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 注册账号
     */
    @PostMapping("/register")
    // @RequiresPermissions("member:member:list")
    public R register(@RequestBody UserRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (UserNameExistException e) {
            return R.error(BizErrorCode.USERNAME_EXIST_ERROR);
        } catch (PhoneExistException e) {
            return R.error(BizErrorCode.PHONE_EXIST_ERROR);
        }
        return R.ok();
    }

    /**
     * 社交账号微博登录
     */
    @PostMapping("/oauth2/weibo/login")
    // @RequiresPermissions("member:member:list")
    public R oauth2WeiboLogin(@RequestBody SocialUserTo to) {

        MemberEntity member = memberService.oauth2WebiBoLogin(to);
        return R.ok().setData(member);

    }

    /**
     * 社交账号登录
     */
    @PostMapping("/oauth2/login")
    // @RequiresPermissions("member:member:list")
    public R oauth2Login(@RequestBody AuthUser to) {

        MemberEntity member = memberService.oauth2Login(to);
        return R.ok().setData(member);

    }

    /**
     * 登录
     */
    @PostMapping("/login")
    // @RequiresPermissions("member:member:list")
    public R login(@RequestBody UserLoginVo vo) {

        MemberEntity member = memberService.login(vo);
        if (member != null) {
            // 说明登录验证通过
            return R.ok().setData(member);
        }
        return R.error(BizErrorCode.ACCT_NO_OR_PASSWORD_ERROR);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
